<?php

/**
  * Framework_Module_CP_Skins 
  *
  * @author      Ian Szewczyk <roamzero@gmail.com>
  * @copyright   (c) 2007 Ian Szewczyk. All rights reserved.
  * @license     http://www.opensource.org/licenses/gpl-license.php 
  * @package Modules
  * @filesource
  */

/**
  * Framework_Module_CP_Skins 
  *
  * @author Ian Szewczyk <roamzero@gmail.com>
  * @package Modules
  */
class Framework_Module_CP_Skins extends Framework_Module_CP
{
	/**
     * $css
     *
     * @access      private
     * @var         array		$css      Array of the css files for the current skin
     */ 
	private $css;
	
	/**
     * $skins
     *
     * @access      private
     * @var         array		$skins     Array of the available skins
     */ 
	private $skins;

	
	/**
     * __construct
     *
     * Initializes the module: sets the default presenter, the allowed
     * conrollers, and the allowed events
     *
     * @access 		public
     * @return 		void
     */
	public function __construct()
	{
		parent::__construct();
	    $this->tplFile = 'Skins.tpl';
	}

	/**
     * __default
     *
     * Generates skins form
     *
     * @access 		public
     * @return 		mixed 		boolean true on success, PEAR_Error on failure
     */
	public function __default()
	{
		$this->forms['lookAndFeel'] = Framework_Quickform::factory('lookAndFeel', 'post', Framework::$request->getBaseHTTPPath().'/'.$this->owner->userName.'/CP/Skins', null, null, true);
		 
		$skinFiles = scandir(FRAMEWORK_BASE_PATH.'/Framework/Site/'.Framework::$site->name.'/Templates');
		 
		$currentSkin = $this->owner->skin;
		$currentSkinDefault = 0;
		$currentCSS  = $this->owner->cssFile;
		$currentCSSDefault = 0;
		 
		$cssFile = array();
		 
		$count = 0;
		foreach ($skinFiles AS $val) {
			if (preg_match('/^[_A-Z0-9]+$/i',$val)){
				$this->skins[$count] = $val;
				if ($val == $currentSkin) $currentSkinDefault = $count;
     
				$cssFiles = @scandir(Framework::$site->getFilePath().'/file/skins/'.$val.'/css');
				if (is_array($cssFiles) && count($cssFiles)) {
					$this->css[$count][0] = 'default.css';

					$cssCount = 1;

					foreach ($cssFiles AS $cssVal){
						if ($cssVal == $currentCSS && $val == $currentSkin){ $currentCSSDefault = $cssCount;}
						if ($cssVal != 'print.css' && $cssVal != 'default.css' && preg_match('/^[_A-Z0-9]+\.css$/i',$cssVal)){
							$this->css[$count][$cssCount] = $cssVal;
							$cssCount++;
						}
					}
					$count++;
				}
			}

		}
		 
		$this->forms['lookAndFeel']->addElement('file', 'bannerFile', 'Upload Banner:', 'class="textInput"');
		$this->forms['lookAndFeel']->addRule('bannerFile', 'Cannot exceed 100KB', 'maxfilesize', 102400);
		$this->forms['lookAndFeel']->addRule('bannerFile', 'Must be a JPEG or GIF', 'mimetype', array('image/jpeg','image/jpg','image/gif','image/pjpeg'));
		 
		$sel = $this->forms['lookAndFeel']->addElement('hierselect','selectSkin','Select Skin: ');
		$sel->setMainOptions($this->skins);
		$sel->setSecOptions($this->css);

		// Set defaults
		$this->forms['lookAndFeel']->setDefaults(array('selectSkin'=>array($currentSkinDefault,$currentCSSDefault)));

		//Set css if user has one, also give edit option with save button, and remove button
		if($this->owner->customCSS) {
			$cssFile = $this->owner->getCustomCSS();
			 
			$fp = @fopen(Framework::$site->getFilePath().$cssFile, 'r');
			$data = @fread($fp, filesize(Framework::$site->getFilePath().$cssFile));
			@fclose($fp);
			if ($data) {
				$this->forms['lookAndFeel']->addElement('textarea','editCSS','Edit CSS: ','class="textInput" id="editCSS" style="width:100%;height:500px"');
				$this->forms['lookAndFeel']->addElement('submit', 'btnSaveCSS', 'Save CSS', 'class="button"');
				$this->forms['lookAndFeel']->addElement('submit', 'btnRemoveCSS', 'Remove CSS',  'class="button"');
				$this->forms['lookAndFeel']->getElement('editCSS')->setValue($data);
				$this->setData('customCSS', true);
			}
		}

		if($this->owner->customHeader) {
			$this->setData('customHeader', Framework::$request->getBaseHTTPPath().$this->owner->getCustomHeader());
		    $this->forms['lookAndFeel']->addElement('submit', 'btnRemoveHeader', 'Remove Header', 'class="button"');
		}

		$this->forms['lookAndFeel']->addElement('file', 'cssFile', 'Upload Custom CSS:',  'class="textInput"');
		$this->forms['lookAndFeel']->addRule('cssFile', 'Cannot exceed 50KB', 'maxfilesize', 51200);
		$this->forms['lookAndFeel']->addRule('cssFile', 'Must be a css file', 'mimetype', array('text/plain','text/css'));

		$this->forms['lookAndFeel']->addElement('submit', 'btnSubmit', 'Submit',  'class="button"');



		if ( $this->forms['lookAndFeel']->validate()) {
			// Form is validated, then processes the data
			$this->forms['lookAndFeel']->process(array($this, 'process'), true);
		}
   

	}

	/**
     * process
     *
     *
     * @access 		public
     * @param 		array		$data		Data to process
     * @return 		boolean		true
     */
	public function process($values) {
		 
		if($values['btnRemoveCSS']) {
			$fileManager = $this->owner->getFileManager();
			$fileManager->removeFile('css',$this->owner->userName.'CSS');
			$this->owner->customCSS = null;
			$this->owner->save();
			$this->forms['lookAndFeel']->removeElement('editCSS');
			$this->forms['lookAndFeel']->removeElement('btnSaveCSS');
			$this->forms['lookAndFeel']->removeElement('btnRemoveCSS');
			$this->setData('formResponse', 'Custom CSS removed');
			return true;
		}

		if($values['btnSaveCSS']) {
			$cssFile = $this->owner->getCustomCSS();
			$fp = @fopen(Framework::$site->getFilePath().$cssFile, 'w');
			$data = str_replace('<?',' ',$values['editCSS']);
			@fwrite($fp, $data);
			@fclose($fp);
			$this->setData('formResponse', 'Custom CSS Saved');
			$this->setData('customCSS', true);
			$element = $this->forms['lookAndFeel']->getElement('editCSS');
			$element->setValue($data);
			return true;
		}

		if($values['btnRemoveHeader']) {
			$fileManager = $this->owner->getFileManager();
			$fileManager->removeFile('header',$this->owner->userName.'Header');
			$this->owner->customHeader = null;
			$this->owner->save();
			$this->forms['lookAndFeel']->removeElement('btnRemoveHeader');
			$this->setData('formResponse', 'Custom header removed');
			$this->setData('customHeader', false);
			return true;
		}
	 	
		$headerFile = $this->forms['lookAndFeel']->getElement('bannerFile');
		 
		if ($headerFile->isUploadedFile()) {
			$fileManager = $this->owner->getFileManager();
			$fileManager->removeFile('header',$this->owner->userName.'Header');
			$result = $fileManager->addFileUpload($headerFile,'header',$this->owner->userName.'Header');
			if (PEAR::isError($result)){
				$errors[] = 'Could Not Upload Header';
			} else {
				$this->owner->customHeader =  $result['fileID'];
				$this->setData('customHeader', $result['httpPath']);
			}
		}
		 
		$cssFile = $this->forms['lookAndFeel']->getElement('cssFile');

		if ($cssFile->isUploadedFile()) {
			$fileManager = $this->owner->getFileManager();
			$fileManager->removeFile('css',$this->owner->userName.'CSS');
			$result = $fileManager->addFileUpload($cssFile,'css',$this->owner->userName.'CSS');
			if (PEAR::isError($result)){
				$errors[] = 'Could Not Upload CSS';
			} else {
				$this->owner->customCSS = $result['fileID'];

				$cssFile = $result['fullPath'];

				$fp = @fopen($cssFile, 'r');
				$data = @fread($fp, filesize($cssFile));
				@fclose($fp);
				if ($data) {
					$this->forms['lookAndFeel']->addElement('textarea','editCSS','Edit CSS: ','class="textInput" id="editCSS" style="width:100%;height:500px"');
					$this->forms['lookAndFeel']->addElement('submit', 'btnSaveCSS', 'Save CSS', 'class="button"');
					$this->forms['lookAndFeel']->addElement('submit', 'btnRemoveCSS', 'Remove CSS',  'class="button"');
					$this->forms['lookAndFeel']->getElement('editCSS')->setValue($data);

				}

				$this->setData('customCSS', true);
			}
		}
			
		if($values['selectSkin']) {
			$this->owner->skin = $this->skins[$values['selectSkin'][0]];
			$this->owner->cssFile = $this->css[$values['selectSkin'][0]][$values['selectSkin'][1]];

		}
		 
		$this->owner->save();


		if (!$errors)
		$this->setData('formResponse', 'Skin setting updated successfully');
		else {
			$this->setData('formResponse', 'There were errors: ');
			$this->setData('errors', $errors);
			return;
		}

		if ($redirect) {
			Framework_Header::redirect(Framework::$request->getHTTPPath());
				
		}


	}

  
}



?>